# 用户服务Dockerfile - RedFire框架版本
# ==========================================

# 构建阶段
FROM python:3.11-slim as builder

# 设置工作目录
WORKDIR /app

# 安装系统依赖
RUN apt-get update && apt-get install -y \
    build-essential \
    curl \
    git \
    && rm -rf /var/lib/apt/lists/*

# 复制依赖文件
COPY requirements.txt .
COPY requirements/ requirements/

# 创建虚拟环境并安装Python依赖
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# 升级pip并安装依赖
RUN pip install --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# 运行阶段
FROM python:3.11-slim as runtime

# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONPATH="/app" \
    PATH="/opt/venv/bin:$PATH"

# 安装运行时依赖
RUN apt-get update && apt-get install -y \
    curl \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd -r redfire \
    && useradd -r -g redfire -d /app -s /bin/bash redfire

# 复制虚拟环境
COPY --from=builder /opt/venv /opt/venv

# 设置工作目录
WORKDIR /app

# 复制应用代码
COPY app/ ./app/
COPY config/ ./config/
COPY services/ ./services/
COPY utils/ ./utils/
COPY schemas/ ./schemas/
COPY interfaces/ ./interfaces/
COPY models/ ./models/
COPY repositories/ ./repositories/
COPY security/ ./security/
COPY validation/ ./validation/
COPY exceptions/ ./exceptions/

# 复制共享模块（如果需要）
COPY ../../../shared/ ./shared/

# 创建必要的目录
RUN mkdir -p /var/log /app/data /app/tmp && \
    chown -R redfire:redfire /app /var/log

# 复制启动脚本
COPY deployment/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# 切换到非root用户
USER redfire

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
    CMD curl -f http://localhost:8001/health || exit 1

# 暴露端口
EXPOSE 8001

# 设置元数据标签
LABEL maintainer="RedFire Team <team@redfire.com>" \
      version="2.0.0" \
      description="RedFire User Service - Authentication and User Management" \
      service="user-service" \
      framework="redfire" \
      build_date="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"

# 入口点
ENTRYPOINT ["/entrypoint.sh"]

# 默认命令
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8001", "--workers", "4"]
